package com.lsw.jgyf.ui.fragment;


import com.lsw.dialog.DialogProgress;
import com.lsw.jgyf.httpapi.HttpApi;
import com.lsw.jgyf.inject.Constants;
import com.lsw.jgyf.ui.base.presenter.BasePresenter;
import com.lsw.util.LogUtils;
import com.lsw.util.ToastUtil;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;

/**
 * Created by lsw on 2017/12/7.
 */

public class HomeFragment2Presenter extends BasePresenter<HomeFragment2Contract.View> implements HomeFragment2Contract.Presenter {
    @Inject
    HttpApi httpApi;

    @Inject
    public HomeFragment2Presenter() {
    }

    @Override
    public void getData(int page, String name, String brand_id, String min_price, String max_price, String order_by, String is_contract) {
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .PhoneList(page + "", name, brand_id, min_price, max_price, order_by,is_contract)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    mView.RefreshSucceed();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        if (baseBean.getData().getData().size() < Constants.PAGE_DEFAULT_COUNT) {
                            mView.setData(baseBean.getData().getData(), false);
                        } else {
                            mView.setData(baseBean.getData().getData(), true);
                        }
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                    }
                }, throwable -> {
                    progress.dismiss();
                    mView.RefreshFail();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void getScreen() {
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .getScreen()
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        mView.setScreen(baseBean.getData());
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }
}
